package sy.service.impl;


import java.util.ArrayList;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.dao.BaseDaoI;
import sy.model.TcustomerInfo;
import sy.pageModel.PagecustomerInfo;
import sy.service.TcustomerInfoServiceI;
import sy.util.DateUtil;
@Service("tcustomerInfoService")
public class TcustomerInfoServiceImpl implements TcustomerInfoServiceI {

	private BaseDaoI<TcustomerInfo> customerInfoDao;
	
	
	
	public BaseDaoI<TcustomerInfo> getCustomerInfoDao() {
		return customerInfoDao;
	}

	@Autowired
	public void setCustomerInfoDao(BaseDaoI<TcustomerInfo> customerInfoDao) {
		this.customerInfoDao = customerInfoDao;
	}

	public PagecustomerInfo add(PagecustomerInfo tcustomerInfo) {
		tcustomerInfo.setCustomerid(UUID.randomUUID().toString());
		//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		tcustomerInfo.setCreatetime(DateUtil.format(new Date()));
		TcustomerInfo ci = new TcustomerInfo();
		BeanUtils.copyProperties(tcustomerInfo, ci);
		customerInfoDao.save(ci);
		return tcustomerInfo;
	}

	public PagecustomerInfo edit(PagecustomerInfo tcustomerInfo) {
		TcustomerInfo customerInfo = customerInfoDao.get(TcustomerInfo.class,tcustomerInfo.getCustomerid());
		if(customerInfo != null){
			customerInfo.setCreatetime(DateUtil.format(new Date()));
			BeanUtils.copyProperties(tcustomerInfo, customerInfo);
			customerInfoDao.update(customerInfo);
		}
		return tcustomerInfo;
	}

	public void remove(String ids) {
		for (String id : ids.split(",")) {
			TcustomerInfo t = customerInfoDao.get(TcustomerInfo.class, id);
			if (t != null) {
				customerInfoDao.delete(t);
			}
		}
	}

	public List<PagecustomerInfo> treegrid() {
		List<TcustomerInfo> customerInfoList = customerInfoDao.find("from TcustomerInfo c order by createTime desc");
		List<PagecustomerInfo> pageCiLists = new ArrayList<PagecustomerInfo>();
		for(TcustomerInfo t : customerInfoList){
			PagecustomerInfo pageCi = new PagecustomerInfo();
			BeanUtils.copyProperties(t, pageCi);
			pageCiLists.add(pageCi);
		}
		return pageCiLists;
	}

	public TcustomerInfo get(String tcustomerInfoId) {
		return this.customerInfoDao.get(TcustomerInfo.class, tcustomerInfoId);
	}
	
	

}
